import org.apache.struts2.util.StrutsTypeConverter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

// Struts2支持转换yyyy-MM-dd格式的日期，其他格式须通过格式转换器自行转换
public class DateStrutsTypeConverter extends StrutsTypeConverter {
    @SuppressWarnings("rawtypes")
    @Override
    public Object convertFromString(Map map, String[] strings, Class aClass) {
        if (!aClass.equals(Date.class) || strings == null || strings.length == 0) {
            return null;
        }
        String val = strings[0];
        Date birthdate = null;
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
        try {
            birthdate = fmt.parse(val);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return birthdate;
    }

    @Override
    public String convertToString(@SuppressWarnings("rawtypes") Map map, Object o) {
        return new SimpleDateFormat("yyyy-MM-dd").format(o);
    }
}
